// pages/myShare/myShare.js
import * as API from '../../api/myShareApi';
import * as util from '../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNo: 1,
    pageSize: 20,
    searchWords: '',
    sectionNum: 0,
    test: 1,
    networkError: false,
    customerModal: false,
    custStatus: true,
    placeholder: '输入客户名称/客户编码搜索',
    id: '',
    customList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.fromDetail) {
      this.setData({
        customerModal: true
      })
    } else {
      wx.removeStorageSync('customId');
      wx.removeStorageSync('choosedClient');
      wx.removeStorageSync('selectOrder');
      wx.removeStorageSync('choosedContacts')
    }

  },
  getCustomUserList: function (searchWords) {
    let postBody = {
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize
    }
    if (searchWords) {
      postBody.searchWords = searchWords
    }
    // util.preventMoreClick(this, () => {
    API.customUserList(postBody).then(res => {
      if (res && res.customList) {
        if (res.customList.length > 0) {
          this.setData({
            customList: res.customList,
            networkError: false
          })
        } else {
          this.setData({
            customList: res.customList,
            networkError: true
          })
        }
      }
    }, err => {
      util.toast(err)
    })
    // })
  },
  onHandleSearch: function (e) {
    util.preventMoreClick(this, () => {
      let searchWords = e.detail.replace(/^\s+|\s+$/g, "");
      if (!searchWords) {
        util.toast('请输入搜索关键字')
        return;
      }
      this.setData({
        id: ''
      })
      wx.removeStorageSync('customId');
      wx.removeStorageSync('choosedClient');
      this.getCustomUserList(searchWords);
    })
  },
  clearSearch: function (e) {
    this.setData({
      id: ''
    })
    wx.removeStorageSync('customId');
    wx.removeStorageSync('choosedClient');
    this.getCustomUserList();
  },
  checkCompany: function (e) {
    let id = e.currentTarget.dataset.id || e.target.dataset.id;
    let code = e.currentTarget.dataset.code || e.target.dataset.id;
    let name = e.currentTarget.dataset.name || e.target.dataset.id;
    let sharding = e.currentTarget.dataset.sharding || e.target.dataset.id;
    console.log(sharding)
    this.setData({
      id: id
    })
    wx.setStorageSync('customId', this.data.id)
    wx.setStorageSync('choosedClient', {
      id: id,
      code: code,
      name: name,
      shardingId: sharding
    })
  },
  nextTo: function () {
    util.preventMoreClick(this, () => {
      wx.navigateTo({
        url: '/pages/myShare/order/order?customId=' + this.data.id
      })
    })
  },
  // 是否结束分享
  switchCustomer: function () {
    this.setData({
      customerModal: true
    })
  },
  hideCustomModal: function () {
    wx.removeStorageSync('selectOrder');
    this.setData({
      customerModal: false
    })
  },
  toDetail: function () {
    util.preventMoreClick(this, () => {
      this.setData({
        custStatus: false
      })
      wx.navigateBack({
        delta: 1
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!this.data.customerModal) {
      wx.removeStorageSync('choosedClient');
      wx.removeStorageSync('selectOrder');
      wx.removeStorageSync('choosedContacts')
    }
    this.getCustomUserList();
    let customId = wx.getStorageSync('customId');
    if (customId) {
      this.setData({
        id: customId
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})